import {
  reqGetMenuList,
  reqBatchAddMenu,
  reqEditMenu,
  reqBatchDelMenu,
  reqQueryMenuListByRole,
  reqQueryChildrenMenuMaxId,
  reqCheckMenuIdIsExisting,
  reqCheckIsRepeatForMenuTitle,
  reqQueryMenuTree,
} from '@/views/Custom/SystemSettingsMng/MenuMng/api/MenuMngApi';

const actions = {
  // 查询全部菜单信息
  // async getMenuList({ commit }, params) {
  //   let result = await reqGetMenuList(params);
  //   commit('GETMENULIST', result.data.menuIPage);
  // },
  // 获取菜单树结构
  async getMenuTree({ commit }, params) {
    let result = await reqQueryMenuTree(params);
    commit('GETMENUTREE', result.data.menuTree);
    return result.data.menuTree;
  },
  // 菜单信息新增
  async batchAddMenu({ commit }, params) {
    let result = await reqBatchAddMenu(params);
    return result;
  },
  // 菜单编辑
  async editMenu({ commit }, params) {
    let result = await reqEditMenu(params);
    return result;
  },
  // 菜单删除
  async delMenu({ commit }, params) {
    let result = await reqBatchDelMenu(params);
    return result;
  },
  // 根据用户角色返回菜单列表
  async getMenuListByRole({ commit }, params) {
    let result = await reqQueryMenuListByRole(params);
    commit('GETMENULISTBYROLE', result.data.displayMenus);
  },
  // 返回当前菜单的子菜单的最大可用编号
  async getChildrenMenuMaxId({ commit }, params) {
    let result = await reqQueryChildrenMenuMaxId(params);
    commit('GETCHILDRENMENUMAXID', result.data.maxNewId);
    return result.data.maxNewId;
  },
  // 判断输入的根级菜单编号是否存在
  async checkMenuIdIsExisting({ commit }, params) {
    let result = await reqCheckMenuIdIsExisting(params);
    return result;
  },
  // 判断菜单标题是否重复
  async checkIsRepeatForMenuTitle({ commit }, params) {
    let result = await reqCheckIsRepeatForMenuTitle(params);
    return result;
  },
};
const mutations = {
  // 查询全部菜单信息
  GETMENULIST(state, result) {
    state.menuIPage = result;
  },
  GETMENUTREE(state, result) {
    state.menuTree = result;
  },
  // 根据用户角色返回菜单列表
  GETMENULISTBYROLE(state, result) {
    state.displayMenus = result;
  },
  // 返回当前菜单的子菜单的最大可用编号
  GETCHILDRENMENUMAXID(state, result) {
    state.maxNewId = result;
  },
  // 判断菜单标题是否重复
};
const state = {
  menuIPage: {}, // 菜单经过分页查询后返回的集合
  menuList: [], // 所有菜单集合
  displayMenus: [], // 登录用户拥有的角色下的菜单集合
  maxNewId: undefined, // 菜单添加时，会自动返回一个可用的编号
  menuTree: [], // 菜单树--所有菜单
};
const getters = {
  menuIPage(state) {
    return state.menuIPage || [];
  },
  menuList(state) {
    return state.menuIPage ? state.menuIPage.records : [] || [];
  },
  displayMenus(state) {
    return state.displayMenus || [];
  },
  maxNewId(state) {
    return state.maxNewId;
  },
  menuTree(state) {
    return state.menuTree;
  },
};
export default {
  namespaced: true,
  actions,
  mutations,
  state,
  getters,
};
